package com.eastfair.home.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.boot.request.PageParams;
import com.eastfair.builder.ExhibitorFeign;
import com.eastfair.builder.dto.ExhibitorDTO;
import com.eastfair.builder.entity.Exhibitor;
import com.eastfair.builder.vo.ExhibitorVO;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.home.service.HomeServiceProviderSpaceService;
import com.eastfair.venuebooking.api.ServiceProviderSpaceFeign;
import com.eastfair.venueservice.dto.ServiceProviderSpaceDTO;
import com.eastfair.venueservice.dto.ServiceProviderSpaceQuery;
import com.eastfair.venueservice.vo.ServiceProviderSpaceVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 展会服务.
 *
 * @author dqq
 * @date 2022 /8/23
 */
@Service
@Slf4j
public class HomeServiceProviderSpaceServiceImpl implements HomeServiceProviderSpaceService {

    @Resource
    private ServiceProviderSpaceFeign serviceProviderSpaceFeign;

    @Resource
    private ExhibitorFeign exhibitorFeign;

    @Override
    public List<ServiceProviderSpaceVO> queryServiceProviderSpace(ServiceProviderSpaceQuery serviceProviderSpaceQuery) {
        R<List<ServiceProviderSpaceVO>> result = serviceProviderSpaceFeign.queryServiceProviderSpace(serviceProviderSpaceQuery);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public List<String> listSpaceCodesByServiceProviderId(Long serviceProviderId, Long exhibitionManageId) {
        ServiceProviderSpaceQuery serviceProviderSpaceQuery = new ServiceProviderSpaceQuery();
        serviceProviderSpaceQuery.setServiceProviderId(serviceProviderId);
        serviceProviderSpaceQuery.setExhibitionManageId(exhibitionManageId);
        List<ServiceProviderSpaceVO> serviceProviderSpaces = queryServiceProviderSpace(serviceProviderSpaceQuery);
        if (serviceProviderSpaces == null || serviceProviderSpaces.isEmpty()) {
            return Collections.emptyList();
        }
        return serviceProviderSpaces
                .stream()
                .map(ServiceProviderSpaceVO::getSpaceCode)
                .collect(Collectors.toList());
    }

    @Override
    public R<Page<ExhibitorVO>> queryServiceProviderSite(PageParams<ExhibitorDTO> query) {
        log.info("queryServiceProviderSite - 通过展会，主场，展厅查询展位, query={}", query);
        ExhibitorDTO model = query.getModel();
        Page<Exhibitor> page = query.buildPage();
        if (model.getHomeId() != null && model.getExhibitionManageId() != null) {
            // 查询主场在该展会下的展馆编码
            List<String> spaceCodeList = listSpaceCodesByServiceProviderId(model.getHomeId(), model.getExhibitionManageId());
            // 如果主场在展会下没有负责的展厅，则返回空数据
            if (spaceCodeList == null || spaceCodeList.isEmpty()) {
                Page<ExhibitorVO> voPage = new Page<>();
                BeanUtil.copyProperties(page, voPage);
                return R.successDef(voPage);
            }
            // 封装主场在展会中负责的展厅列表到查询条件中
            model.setSpaceCodeList(spaceCodeList);
        }
        log.info("queryServiceProviderSite - 调用搭建商查询展位列表, query={}", query);
        R<Page<ExhibitorVO>> result = exhibitorFeign.queryServiceProviderSite(query);
        log.info("queryServiceProviderSite - 调用搭建商查询展位列表完成, result={}", result);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result;
    }
}